package com.pai.gui.component;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListCellRenderer;

import com.pai.entity.PaiTask;
import com.pai.entity.User;
import com.pai.service.TransFileManager;
/**
 * 用于定义用户cell的样式
 * @author Jeff
 *
 */
public class UserListCell extends JPanel{

	private static final long serialVersionUID = 1L;
	
	private int index = 0;
	
	private UserListCell usercell = this;
	private UserIconList list;
	private User bean;
	
	//界面组件：
	private JLabel iconlable;
	private JLabel namelable;
	private JPopupMenu popmenu;
	private JFileChooser filechooser;
	private TransFileManager tfm;
	private int maxwidth = 200;
	private int maxhight = 80;
	private File file;
	public UserListCell(TransFileManager tfm){
		//this.setSize(200,80);
		this.setOpaque(false);
		this.setLayout(null);
		this.tfm=tfm;
	}
	public UserListCell(){
		//this.setSize(200,80);
		this.setOpaque(false);
		this.setLayout(null);
	}
	
	public TransFileManager getTfm() {
		return tfm;
	}


	public void setTfm(TransFileManager tfm) {
		this.tfm = tfm;
	}

	/**
	 * 通过传入user对象和list对象，生成一个cell，放入list中显示。
	 * @param list 用户list对象
	 * @param value 传入User
	 * @return
	 */
	public JComponent getListCell(UserIconList list, Object value) {
		this.bean = (User)value;
		this.list = list;
		
		iconlable = new JLabel(new ImageIcon("res/icon/jifeng.png"));
		namelable = new JLabel(bean.getUsername());
		namelable.setFont(new java.awt.Font("Arial", 1,12));
		namelable.setForeground(Color.white);
		filechooser = new JFileChooser();
		filechooser.setForeground(Color.white);
		popmenu = new JPopupMenu();
		JMenuItem sendfile = new JMenuItem("send file");
		popmenu.add(sendfile);
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if(e.isPopupTrigger()){
					//显示右键的菜单
					popmenu.show(e.getComponent(), e.getX(), e.getY());
				}
			}			
		});
		
		sendfile.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String ip = bean.getUserIP();
				int port = bean.getUserPort();
				
				int returnVal = filechooser.showOpenDialog(usercell);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					file = filechooser.getSelectedFile();
					System.out.println("choose :" + file.getAbsolutePath());
					//生成Paitask对象，存储发送文件信息。
					PaiTask task = new PaiTask();
	                task.setFileName(file.getName());
	                task.setFileSize(file.length());
	                task.setBeginTime(new Date());
	                task.setFileSrc(file.getAbsolutePath());
	                task.setFromUser(bean);
	                
	                //利用tfm发送文件。
					tfm.sendFile(bean.getUserIP(), bean.getUserPort(), file.getAbsolutePath(), null,task);
					//product
					//tfm.sendFile(ip, port, file.getAbsolutePath(), null,task);
					System.out.println("send file;");
				}
				else{
					System.out.println("cancle file choose");
				}

			}
			
		});
		
		iconlable.setBounds(15, 15, 50, 50);
		namelable.setBounds(80, 15, 100, 50);
		//namelable.setForeground(Color.red);
		this.add(iconlable);
		this.add(namelable);
		this.add(popmenu);
		//this.setBounds(0,0,200,10);
		//this.setPreferredSize(200,10);
		this.setMaximumSize(new Dimension(800, 80));

		//this.setResizable(false);
		return this;
	}
	public void setSelect(boolean iss) {
		
	}
	
}
